#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:'手机视界' 
@file: start_uwsgi.py
@time: 2018/09/22

uwsgi --chdir=/path/to/your/project \
    --module=mysite.wsgi:application \
    --env DJANGO_SETTINGS_MODULE=mysite.settings \
    --master --pidfile=/tmp/project-master.pid \
    --socket=127.0.0.1:49152 \      # can also be a file
    --processes=5 \                 # number of worker processes
    --uid=1000 --gid=2000 \         # if root, uwsgi can drop privileges
    --harakiri=20 \                 # respawn processes taking more than 20 seconds
    --max-requests=5000 \           # respawn processes after serving 5000 requests
    --vacuum \                      # clear environment on exit
    --home=/path/to/virtual/env \   # optional path to a virtualenv
    --daemonize=/var/log/uwsgi/yourproject.log      # background the process
"""
from configparser import ConfigParser
import os

CONF = ConfigParser()

CUR_DIR = os.path.curdir
UWSGI_NAME = 'uwsgi.ini'
CONF_PATH = os.path.join(CUR_DIR, UWSGI_NAME)
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
CONF.read(CONF_PATH)


def write_conf(conf_path):
    CONF.write(open(conf_path, 'w'))


class UwsgiConfig(object):
    def __init__(self):
        self.u_section = 'uwsgi'

    def init_uwsgi_conf(self):
        u_conf = {
            'uwsgi': {'chdir': BASE_PATH, 'module': CUR_DIR + '.wsgi:application', 'socket': '127.0.0.1:8000',
                      'processes': 5,
                      'max-requests': 5000, 'daemonize': CONF_PATH + 'uwsgi.log'}}

        CONF.read_dict(u_conf)
        write_conf(CONF_PATH)


if __name__ == '__main__':
    uw = UwsgiConfig()
    uw.init_uwsgi_conf()
